package commons

import "strings"

type Err struct {
	Error     string `json:"error"`
	ErrorCode string `json:"error_code"`
}

type ErrResponse struct {
	HttpStatusCode int
	Error          Err
}

/*
状态码

服务器向用户返回的状态码和提示信息，使用标准的HTTP状态码

200 OK 服务器成功返回用户请求的数据
401 Unauthorized 表示用户没有认证[登陆]，无法进行当前操作
403 Forbidden 表示用户性错误比如参数错误,操作失败等...
500 INTERNAL SERVER ERROR 服务器内部错误，此时服务端无法处理任何请求。
*/

var (
	ErrorRequestBodyParseFailed = ErrResponse{HttpStatusCode: 403, Error: Err{Error: "请求错误", ErrorCode: "001"}}
	ErrorUnknownError           = ErrResponse{HttpStatusCode: 403, Error: Err{Error: "未知错误", ErrorCode: "002"}}
	ErrorNotAuthUser            = ErrResponse{HttpStatusCode: 401, Error: Err{Error: "你已登出，请重新登陆", ErrorCode: "003"}}
	ErrorNotPermission          = ErrResponse{HttpStatusCode: 403, Error: Err{Error: "无访问权限", ErrorCode: "010"}}
	ErrorPageNoFound            = ErrResponse{HttpStatusCode: 404, Error: Err{Error: "页面不存在", ErrorCode: "004"}}
	ErrorDBError                = ErrResponse{HttpStatusCode: 500, Error: Err{Error: "数据库错误", ErrorCode: "005"}}
	ErrorServerError            = ErrResponse{HttpStatusCode: 500, Error: Err{Error: "服务内部错误", ErrorCode: "006"}}
	ErrorParamError             = ErrResponse{HttpStatusCode: 403, Error: Err{Error: "参数错误", ErrorCode: "007"}}
	ErrorUserExits              = ErrResponse{HttpStatusCode: 403, Error: Err{Error: "用户已存在", ErrorCode: "008"}}
	ErrorUserNotExits           = ErrResponse{HttpStatusCode: 403, Error: Err{Error: "用户不存在", ErrorCode: "009"}}
	ErrorDataNotExits           = ErrResponse{HttpStatusCode: 403, Error: Err{Error: "数据不存在", ErrorCode: "011"}}
	ErrorActionFail             = ErrResponse{HttpStatusCode: 403, Error: Err{Error: "操作失败", ErrorCode: "012"}}
	ErrorPassword               = ErrResponse{HttpStatusCode: 403, Error: Err{Error: "密码错误", ErrorCode: "013"}}
	ErrorLoginFail              = ErrResponse{HttpStatusCode: 403, Error: Err{Error: "登陆失败", ErrorCode: "014"}}
	ErrorEquipmentNoExists      = ErrResponse{HttpStatusCode: 403, Error: Err{Error: "设备不存在", ErrorCode: "015"}}
	ErrorFastFail               = ErrResponse{HttpStatusCode: 500, Error: Err{Error: "操作频率过快，请稍后", ErrorCode: "016"}}
)

// 自定义错误
func CustomError(code int, err error) ErrResponse {
	return ErrResponse{HttpStatusCode: code, Error: Err{Error: delError(err), ErrorCode: "-1"}}
}

// 用户请求错误
func UserError(err error) ErrResponse {
	return ErrResponse{HttpStatusCode: 403, Error: Err{Error: delError(err), ErrorCode: "001"}}
}

// 服务器错误
func ServerError(err error) ErrResponse {
	return ErrResponse{HttpStatusCode: 500, Error: Err{Error: delError(err), ErrorCode: "006"}}
}

// 权限错误 - 登出或未登陆
func AuthError(err error) ErrResponse {
	return ErrResponse{HttpStatusCode: 401, Error: Err{Error: delError(err), ErrorCode: "003"}}
}

// 权限错误 - 无访问权限
func PermissionError(err error) ErrResponse {
	return ErrResponse{HttpStatusCode: 403, Error: Err{Error: delError(err), ErrorCode: "010"}}
}

// 处理错误
func delError(err error) string {
	strArr := strings.Split(err.Error(), "|")
	return strings.TrimSpace(strArr[len(strArr)-1])
}
